﻿package scrn;


import java.awt.*;
import java.awt.image.BufferedImage;

import javax.swing.*;


public class Test02 extends JFrame {

public Test02() {

	setDefaultCloseOperation(EXIT_ON_CLOSE);
	setPreferredSize(new Dimension(150, 150));

	JScrollPane sp = new JScrollPane();
	JList list = new JList();
	ImagePanel panel = new ImagePanel();

	sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
	sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

	sp.setOpaque(false);
	sp.getViewport().setOpaque(false);
	list.setOpaque(false);
	panel.setOpaque(true);

	list.setCellRenderer(new CellRenderer());
	String[] lData = new String[10];
	for (int i = 0; i < 10; i++) {
		lData[i] = "Line " + (i + 1);
	}
	list.setListData(lData);
	list.setSelectedIndex(0);
	panel.add(list);

	sp.getViewport().setView(panel);
	getContentPane().add(sp);
	pack();
}

class CellRenderer extends DefaultListCellRenderer {

@Override
public boolean isOpaque() {

	return false;
}


@Override
public Component getListCellRendererComponent(JList list, Object value,
		int index, boolean isSelected, boolean cellHasFocus) {

	Component c = super.getListCellRendererComponent(list, value, index,
			isSelected, cellHasFocus);
	((JLabel) c).setForeground(Color.WHITE);
	return c;
}
}

class ImagePanel extends JPanel {

Image img;


public ImagePanel() {

	img = makeImage();
}


public void paintComponent(Graphics g) {

	super.paintComponent(g);
	Rectangle r = ((JViewport) this.getParent()).getViewRect();
	g.drawImage(img, r.x, r.y, r.width, r.height, null);
}
}


private Image makeImage() {

	BufferedImage bi = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);
	Graphics g = bi.getGraphics();
	int[] curColor = new int[3];
	int[] colors = { 255, 0, 0, 0, 255, 0, 0, 0, 255 };
	for (int i = 0; i < 3; i++) {
		System.arraycopy(colors, i * 3, curColor, 0, 3);
		for (int j = 0; j < 6; j++) {
			curColor[i] -= 15;
			Color c = new Color(curColor[0], curColor[1], curColor[2]);
			g.setColor(c);
			drawBox(g, j * 50, i * 100, 50, 100);
		}
	}
	return Toolkit.getDefaultToolkit().createImage(bi.getSource());
}


private void drawBox(Graphics g, int x, int y, int width, int height) {

	for (int i = 0; i < height; i++) {
		g.drawLine(x, y + i, x + width, y + i);
	}
}


public static void main(String[] args) {

	new Test02().setVisible(true);
}
}
